﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AMatysiak.Controls
{
    [DefaultEvent("ValueBarChanged")]
    public partial class ValueBar : UserControl
    {
        private double min;
        [DefaultValue(0)]
        public double Min
        {
            get { return min; }
            set { handleMinValueChanged(value); }
        }

        private void handleMinValueChanged(double value)
        {
            double old = min;
            min = value;
            actualizeTrackBar(old, Max, Step);
            lblMin.Text = Min.ToString();
        }

        private void actualizeTrackBar(double oldMin, double oldMax, double oldStep)
        {
            double value = (trackBar.Value + (oldMin / oldStep)) * oldStep;

            trackBar.Minimum = 0;
            trackBar.Maximum = (int)((Max - Min) / Step);

            if (!value.Equals(double.NaN))
            {
                if (value > Max && oldMax > Max)
                    Value = Max;
                else if (value < Min && oldMin < Min)
                    Value = Min;
                else
                    Value = value;
            }

            trackBar.SmallChange = 1;
            trackBar.LargeChange = (Max < Min ? 1 : (int)((Max - Min) / 10));
        }

        private double max;
        [DefaultValue(10)]
        public double Max
        {
            get { return max; }
            set { handleMaxValueChanged(value); }
        }

        private void handleMaxValueChanged(double value)
        {
            double old = max;
            max = value;
            actualizeTrackBar(Min, old, Step);
            lblMax.Text = Max.ToString();
        }

        private double step;
        [DefaultValue(1)]
        public double Step
        {
            get { return step; }
            set { handleStepValueChanged(value); }
        }

        private void handleStepValueChanged(double value)
        {
            double old = step;
            step = value;
            actualizeTrackBar(Min, Max, old);
        }

        public double Value
        {
            get { return (trackBar.Value + (Min / Step)) * Step; }
            set { trackBar.Value = (int)((value - Min) / Step); }
        }

        public ValueBar()
        {
            InitializeComponent();
        }

        public bool ShowMinMaxLabels
        {
            get { return lblMax.Visible; }
            set
            {
                lblMax.Visible = value;
                lblMin.Visible = value;
            }
        }

        public delegate void ValueBarChangedEventArgs(double value);
        public event ValueBarChangedEventArgs ValueBarChanged;

        private void trackBar_ValueChanged(object sender, EventArgs e)
        {
            if (ValueBarChanged != null)
                ValueBarChanged(Value);
        }
    }
}
